package com.hu.controller;

import com.hu.pojo.Dept;
import com.hu.pojo.Result;
import com.hu.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j    //获取日志记录对象
@RestController

public class DeptController {

    @Autowired
    private DeptService deptService;

    /*查询全部（导入）*/
    @GetMapping("/depts")//请求路径,指定请求路径为get
    public Result list(){
        //输出日志
        log.info("查询全部部门数据");
        //调用service层
        List<Dept> deptList =  deptService.list();
        return Result.success(deptList);
    }
    /*删除数据*/
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);//日志
        deptService.delete(id);//service层
        return Result.success();
    }

    /*新增部门*/
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}", dept);//日志
        deptService.add(dept);
        return Result.success();
    }

    /*编辑部门*/
    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询部门：{}",id);//日志
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);//日志
        deptService.update(dept);
        return Result.success();
    }
}
